﻿using System;
using System.IO;
using System.Net.Sockets;

namespace treadmillProject.src.model {
    public class Client {
        private TcpClient client;
        private string hostname = "127.0.0.1";
        private const Int32 port = 2055;

        private Stream s;
        private StreamReader sr;
        private StreamWriter sw;


        private const int waitForTrainingData = 1;
        private static int state;

        private string name;

        public Client() {
            Console.WriteLine("client init...");
            state = waitForTrainingData;
            //clientInit();
        }

        public string getNextLine() {
            return sr.ReadLine();
        }


        public void clientInit() {
            //TcpClient client = new TcpClient("Michael-PC", 2055);
            client = new TcpClient(hostname, port);

            try {
                s = client.GetStream();
                sr = new StreamReader(s);
                sw = new StreamWriter(s);
                sw.AutoFlush = true;
                Console.WriteLine("CLIENT IS ALIVE!");
                //sendstart();
                //sendstart();
            } catch (Exception e) {
                Console.WriteLine(e.StackTrace);
                Console.WriteLine("Failed To connect");
                //client.Close();
            }
        }

        public void sendstart() {
            try {
                sw.WriteLine("[");
            } catch (Exception) {
                Console.WriteLine("can not sendstart ");
            }
        }


        public void sendstop() {
            try {
                sw.WriteLine("]");
            } catch (Exception) {
                Console.WriteLine("can not sendstop ");
            }
        }

        public void sendGear(int g) {
            sw.WriteLine("<g" + g + ">");
        }

        public void client_end() {
            try {
                //sendstop();
                //sendstop();
                s.Close();
                client.Close();
            } catch (Exception e) {
                Console.WriteLine("Can not close connection");
            }
        }
    }
}